<?php
class ExportController extends Controller {
	
	private $_expListArray = array();
	private $_dictId = 0;
	
	public function indexAction() {
        $this->checkLoginRedirect();
		$dicts = $this->_model->getAllDict();
		$this->_view->assignArray(array(
			'pageTitle' => 'Localization system : export leksems step 1',
			'dicts' => $dicts,
            'pageId' => 'step1'
		));
		$this->_view->render('export/step1');
	}
	
	public function generateAction() {
        $this->checkLoginRedirect();
		$this->_dictId = $this->_request->getString('dict');
		$wsId = $this->_session->getInt('websiteId');
		$leks = $this->_model->getAllLeks($wsId);
		$isArchived = ($this->_request->getString('isArchived')) ? true : false;
		$fileName = ($this->_request->getString('langFileName')) ? $this->_request->getString('langFileName') : '';
		foreach ($leks as $lek) {
			if(!$lek->isTranslated() && $this->_dictId != 'base') {
				continue;
			}
			$translator = new Translator($this->_dictId, $lek->id);
			$this->_expListArray[$lek->id] = array(
				'name' => $lek->name,
				'value' => $translator->getTranslation()
			);
		}
		$this->_session->set('isArchived', $isArchived);
		$this->_makeUserExportDir();
		$this->_makeExportFile($fileName, $isArchived);
		$this->_makeCsvFile($fileName, $isArchived);
		$this->delegateToAction('step2');
	}
	
	public function step2Action() {
        $this->checkLoginRedirect();
        $dictionary = new Dictionary($this->_dictId);
      	$lang = ($this->_dictId == 'base') ? 'english' : $dictionary->name;
		$user = $this->_session->getCurrent();
		$filePath = str_replace(Config::$path, '', ($this->_session->getBool('isArchived')) ? $this->_session->getString('zipFilePath') : $this->_session->getString('filePath'));
		$csvFilePath = str_replace(Config::$path, '', ($this->_session->getBool('isArchived')) ? $this->_session->getString('csvZipFilePath') : $this->_session->getString('csvFilePath'));
		$this->_view->assignArray(array(
			'pageTitle' => 'Localization system : export leksems step 2',
			'prevLeks' => $this->_expListArray,
			'fileLink' => $filePath,
			'csvFileLink' => $csvFilePath,
            'pageId' => 'step2'
		));
		$this->_view->render('export/step2');
	}
	
	protected function _makeCsvFile($fileName = '', $isArchived = false) {
		$dictionary = new Dictionary($this->_dictId);
      	$lang = ($this->_dictId == 'base') ? 'english' : $dictionary->name;
		$user = $this->_session->getCurrent();
		$fileContent = '';
		if(!$this->_expListArray) {
			return;
		}
		foreach ($this->_expListArray as $expItem) {
			$fileContent .= $expItem['name'] . ';' . $expItem['value'] . "\n";
		}
		if($fileName) {
			$filePath = Config::$path . '/' . Config::$expDir . '/user_' . $user->id . '/' . $fileName . '.csv';
		}
		else {
			$filePath = Config::$path . '/' . Config::$expDir . '/user_' . $user->id . '/' . $lang . '.csv';
		}
		file_put_contents($filePath, $fileContent);
		if($isArchived) {
			$zip = new ZipArchive();
			if($fileName) {
				$zipName = Config::$path . '/' . Config::$expDir . '/user_' . $user->id . '/' . $fileName . '.zip';
			}
			else {
				$zipName = Config::$path . '/' . Config::$expDir . '/user_' . $user->id . '/' . $lang . '.zip';
			}
			if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== true) {
				die();
			}
			$zip->addFile($filePath, (!empty ($fileName)) ? '/' . $fileName . '.csv' : '/' . $lang . '.csv');
			$zip->close();
			$this->_session->set('csvZipFilePath', $zipName);
		}
		$this->_session->set('csvFilePath', $filePath);
	}

	protected function _makeExportFile($fileName = '', $isArchived = false) {
		$dictionary = new Dictionary($this->_dictId);
      	$lang = ($this->_dictId == 'base') ? 'english' : $dictionary->name;
      	
		$fileContent = "<?php \n";
		if(!$this->_expListArray) {
			return;
		}
		$fileContent .= "\$translator = array( \n";
		foreach($this->_expListArray as $expLek) {
			$fileContent .= "\t '".htmlspecialchars($expLek['name'])."' => '".htmlspecialchars($expLek['value'])."', \n";
		}
		$fileContent .= ");";
		$user = $this->_session->getCurrent();
		
		if($fileName) {
			$filePath = Config::$path . '/' . Config::$expDir . '/user_' . $user->id . '/' . $fileName . '.php';
		}
		else {
			$filePath = Config::$path . '/' . Config::$expDir . '/user_' . $user->id . '/' . $lang . '.php';
		}
		file_put_contents($filePath, $fileContent);
		if($isArchived) {
			$zip = new ZipArchive();
			if($fileName) {
				$zipName = Config::$path . '/' . Config::$expDir .'/user_' . $user->id . '/' . $fileName . '.zip';
			}
			else {
				$zipName = Config::$path . '/' . Config::$expDir . '/user_' . $user->id . '/' . $lang . '.zip';
			}
			if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== true) {
				die();
			}
			$zip->addFile($filePath, (!empty ($fileName)) ? '/' . $fileName . '.php' : '/' . $lang . '.php');
			$zip->close();
			$this->_session->set('zipFilePath', $zipName);
		}
		$this->_session->set('filePath', $filePath);
	}

	private function _makeUserExportDir() {
		$previewPath = Config::$path . '/' . Config::$expDir . '/';
		$user = $this->_session->getCurrent();
		if(!file_exists($previewPath . 'user_' . $user->id) || !is_dir($previewPath . 'user_' . $user->id)) {
			mkdir($previewPath . 'user_' . $user->id);
		}
	}
}